package com.open.cloud.rbac.controller.role;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.entity.role.CoreRole;
import com.open.cloud.rbac.entity.role.CoreRolePermissionRel;
import com.open.cloud.rbac.object.role.*;
import com.open.cloud.rbac.service.role.ICoreRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-角色业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/role")
public class CoreRoleController {

    @Resource
    private ICoreRoleService roleService;

    @ApiOperation(value = "创建")
    @PostMapping("/add")
    public ResponseWrapper addPostInfo(@RequestBody CoreRoleDto obj) {
        JSONObject data = roleService.addRoleInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public ResponseWrapper updatePostInfo(@RequestBody CoreRoleDto obj) {
        JSONObject data = roleService.updateRoleInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "按ID删除")
    @GetMapping("/delete")
    public ResponseWrapper deletePostById(@RequestParam(name = "id") String id,
                                          @RequestParam(name = "isforce", required = false, defaultValue = "false") Boolean isforce) {
        JSONObject data = roleService.deleteRoleInfo(id, isforce);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public ResponseWrapper<IPage<CoreRoleVo>> getRolePage(@RequestBody CoreRoleSelectDto selectDto) {
        IPage<CoreRoleVo> data = roleService.getRolePage(selectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "列表查询")
    @PostMapping("/list")
    public ResponseWrapper<List<CoreRole>> getRoleList(@RequestBody CoreRoleSelectDto selectDto) {
        List<CoreRole> data = roleService.getRoleList(selectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "角色权限列表查询")
    @GetMapping("/permission/rel/list")
    public ResponseWrapper<List<CoreRolePermissionRel>> getRolePermissionRelList(@RequestParam(name = "roleId") String roleId) {
        List<CoreRolePermissionRel> data = roleService.getRolePermissionRelList(roleId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "修改角色权限配置")
    @PostMapping("/change/permission/rel")
    public ResponseWrapper<List<CoreRole>> changePermissionRel(@RequestBody CoreRolePermissionDto rolePermissionDto) {
        JSONObject data = roleService.setRolePermissionRel(rolePermissionDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "根据orgId查询roleId列表")
    @GetMapping("/org/rel/role/list")
    public ResponseWrapper<List<String>> getRoleIdsByOrgId(@RequestParam(name = "orgId") String orgId) {
        List<String> data = roleService.getOrgRelRoleByOrgId(orgId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "组织分配角色")
    @PostMapping("/org/alloc/role")
    public ResponseWrapper allocOrgRoleRel(@RequestBody CoreRoleOrgRelDto roleOrgRelDto) {
        JSONObject data = roleService.setOrgRelRole(roleOrgRelDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "根据postId查询roleId列表")
    @GetMapping("/post/rel/role/list")
    public ResponseWrapper<List<String>> getRoleIdsByPostId(@RequestParam(name = "postId") String postId) {
        List<String> data = roleService.getRoleIdsRelPostByPostId(postId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "岗位分配角色")
    @PostMapping("/post/alloc/role")
    public ResponseWrapper allocPostRoleRel(@RequestBody CoreRolePostRelDto rolePostRelDto) {
        JSONObject data = roleService.setPostRelRole(rolePostRelDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "根据userId查询roleId列表")
    @GetMapping("/user/rel/role/list")
    public ResponseWrapper<List<String>> getRoleIdsByUserId(@RequestParam(name = "userId") String userId) {
        List<String> data = roleService.getRoleIdsRelUserByUserId(userId);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "用户分配角色")
    @PostMapping("/user/alloc/role")
    public ResponseWrapper allocUserRoleRel(@RequestBody CoreRoleUserRelDto roleUserRelDto) {
        JSONObject data = roleService.setUserRelRole(roleUserRelDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

}

